package Classes.GameViews.ViewDisplayers.MapTargets// ActionScript file
{
	import Classes.GameDatas.GD_HavenWall;
	import Classes.GameDatas.GD_MapGrid;
	import Classes.GameDatas.GameData;
	import Classes.GameUtils.GU_IsoUtils;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.ViewCartoonTargets.ViewCartoonTarget;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapNPCs.MN_Zombie;
	import Classes.GameViews.ViewDisplayers.ViewDisplayer;
	import Classes.Interfaces.I_Bar;
	import Classes.Managers.M_ConfigData;
	import Classes.Managers.M_LoadSWFSource;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	/**
	 *地图对象类 
	 * @author 葛增文
	 * 
	 */	
	public class MapTarget extends ViewDisplayer implements I_Bar
	{
		private var _mapGrid:GD_MapGrid;/*所在地图格*/
		private var _coordinateTxt:TextField;/*坐标显示框*/
		
		/** 2012.4.19 update by jamesge **/
		/**
		 * 移除地图对象的标记  1：从地图上面移除  2：从地图上面移除前面的状态，需要把新的状态再次加入地图对象中 
		 */		
		private var _removeSign:Number=0;/*移除标记*/
		/** 2012.4.19 update by jamesge **/
		
		protected var _deepIndex:Number;
		public var txSp:Sprite;		//特效显示sprite
		
		/**
		 *是否被选中为目标
		 */
		public var _isSelect:Boolean	=	false;
		public function MapTarget(gd:GameData):void
		{
			super(gd);
			this.init();
		}
		
		private function init():void
		{
			this.addListener();
			/*this._coordinateTxt=new TextField();
			this.coordinateTxt.width=50;
			this.coordinateTxt.mouseEnabled=false;
			this.addChild(this.coordinateTxt);*/
		}
		
		private function addListener():void
		{
			this.addEventListener(Event.ADDED_TO_STAGE,addHandle);
			this.addEventListener(Event.REMOVED_FROM_STAGE,removeHandle);
		}
		
		private function removeListener():void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,addHandle);
			this.removeEventListener(Event.REMOVED_FROM_STAGE,removeHandle);
		}
		
		private function addHandle(e:Event):void
		{
			this.addToMapHandle();
		}
		
		private function removeHandle(e:Event):void
		{
			this.removeFromMapHandle();
		}
		
		protected function addToMapHandle():void
		{
			
		}
		
		protected function removeFromMapHandle():void
		{
			
		}
		
		public function getVertexsAry():Array{return null};
		
		/**
		 *设定位置 
		 * @param mapGrid 设定地图格
		 * 
		 */		
		public function setPosition(mapGrid:GD_MapGrid):void
		{
			this.mapGrid=mapGrid;
			var isoPoint:Point=new Point(mapGrid.atx*GV_Map.instance.gridSize,mapGrid.aty*GV_Map.instance.gridSize);
			var screenPoint:Point=GU_IsoUtils.isoToScreen(isoPoint);
			this.x=screenPoint.x;
			this.y=screenPoint.y;
			
			this.updataDeepIndex();
		}
		
		/**
		 *创建地图对象的抛出奖励 
		 * @param rewardsAry
		 * 
		 */		
		public function creatRewards(rewardsAry:Array):void
		{
			for each(var rewardObj:Object in rewardsAry)
			{
				var actObj:Object=new Object;
				actObj.atx=this.x;
				actObj.aty=this.y;
				actObj.vx=Math.random()*(-8)+4;
				actObj.vy=-30;
				actObj.excursiony=Math.random()*(-6)+3;
				GV_Map.instance.creatReward(rewardObj,actObj);
			}
		}
		
		/**
		 *碰撞检测 
		 * @return 
		 * 
		 */		
		public function hitTestPointCheck(checkX:Number,checkY:Number):Boolean
		{
			return this.hitTestPoint(checkX,checkY);
		}
		
		/**
		 *对象被点击 
		 * @param e
		 * 
		 */		
		public function beClicked(e:MouseEvent):void
		{
			
		}
		
		/**
		 *设定半透明 
		 * @param value
		 * 
		 */		
		public function setTransparent(value:Boolean):void
		{
			this.alpha=value?0.25:1;
		}
		
		/**
		 *设定半透明 只在非建筑模式下有效GV_Map.instance.buildMode==GV_Map.CANNOTBUILDMODE)
		 * @param value
		 * 
		 */		
		public function setTransparentFromGVMap(value:Boolean):void
		{
			if( GV_Map.instance.buildMode==GV_Map.CANNOTBUILDMODE)
			this.alpha=value?0.25:1;
		}
		
		/**
		 *设定是否选中为目标 
		 * @param v
		 * 
		 */
		public function set isSelect(v:Boolean):void
		{
			this._isSelect	=	v;
			if(this._isSelect)
			{
				var filter:GlowFilter=new GlowFilter(0xff0000,0.8,10,10,2,BitmapFilterQuality.LOW);
				var filters:Array=[filter];
				this.filters=filters;
			}
			else
			{
				this.filters=null;
			}
			
//			if(this is MN_Zombie)
//			{
//				if(this["hotPanel"])
//					this["hotPanel"].visible=v;
//			}
		}
		
		/**
		 *设定发光效果 
		 * @param value
		 * 
		 */		
		public function setGlowFilter(value:Boolean):void
		{
			if(_isSelect){
				this.isSelect=true;
				return;
			}
			
			if(_isWaitSelect){
				this.isWaitSelect=true;
				return;
			}
//			
			if(value)
			{
				var filter:GlowFilter=new GlowFilter(0xffffff,0.8,10,10,2,BitmapFilterQuality.LOW);
				var filters:Array=[filter];
				this.filters=filters;
			}
			else
			{
				this.filters=null;
			}
		}
		
		private var _isWaitSelect:Boolean=false;
		public function set isWaitSelect(v:Boolean):void
		{
			this._isWaitSelect	=	v;
			if(this._isWaitSelect)
			{
				var filter:GlowFilter=new GlowFilter(0xaa0000,0.8,10,10,2,BitmapFilterQuality.LOW);
				var filters:Array=[filter];
				this.filters=filters;
			}
			else
			{
				this.filters=null;
			}
			
//			if(this is MN_Zombie)
//			{
//				if(this["hotPanel"])
//					this["hotPanel"].visible=v;
//			}
		}
		/**
		 *显示坐标 
		 * 
		 */		
		/*protected function showCoordinate():void
		{
		this.setChildIndex(this.coordinateTxt,this.numChildren-1);
		this.coordinateTxt.text=this.mapGrid.atx+","+this.mapGrid.aty;
		}*/
		
		override public function redraw():void
		{
			super.redraw();
			this.removeSign=0;
		}
		
		/**
		 *释放 
		 * 
		 */		
		override public function release():void
		{
			releaseStates();
			
			this.removeListener();
			super.release();
			this.removeSign=1;
			if(this._mapGrid)
			this._mapGrid.release();
			this._mapGrid=null;
		}
		
		/*getter & setter*/
		public function get mapGrid():GD_MapGrid
		{
			return _mapGrid;
		}
		
		public function set mapGrid(value:GD_MapGrid):void
		{
			_mapGrid = value;
			//this.showCoordinate();
		}
		
		public function get deepIndex():Number
		{
			return this.y;//+this.x*.001;
		}
		
		public function updataDeepIndex():void
		{
			this._deepIndex		= 	this.y;
		}
		
		public function get deepIndexByX():Number
		{
			return this.x;
		}
		
		public function get actionEnabled():Boolean
		{
			return true;
		}
		
		public function get coordinateTxt():TextField
		{
			return _coordinateTxt;
		}
		
		public function get selectPriority():int
		{
			return 0;
		}
		
		
		/** 2012.4.19 update by jamesge **/
		/**
		 *  移除地图对象的标记  1：从地图上面移除  2：从地图上面移除前面的状态，需要把新的状态再次加入地图对象中  
		 * @return 
		 */		
		public function get removeSign():Number
		{
			return _removeSign;
		}
		/**
		 *  移除地图对象的标记  1：从地图上面移除  2：从地图上面移除前面的状态，需要把新的状态再次加入地图对象中  
		 * @param value
		 */
		public function set removeSign(value:Number):void
		{
			_removeSign = value;
		}
		
		
		
		
		
		
		
		//************************************************** 特效 ****************************
		public var txStMap:Object		= 	{}
		private function clearTX():void
		{
			if(null!=this.txSp){
				while(this.txSp.numChildren)this.txSp.removeChildAt(0);
			}
		}
		public function addTX(txid:uint,delay:uint=0,_rotation:Number=0):Sprite{
			var tsp:TX_sprite;
			txStMap[txid]	=	tsp	 =	new TX_sprite(this,txid,delay);
			tsp._rotation	=	_rotation;
			M_LoadSWFSource.instance.needLoad(tsp,M_ConfigData.instance.imgUrl+TX_sprite.TXObj[txid].zt_tx_source,TX_sprite.TXObj[txid].zt_tx_icon);			
			//			M_LoadSWFSource.instance.needLoad(tsp,M_ConfigData.instance.imgUrl+TX_sprite.TXObj[txid].zt_tx_source, TX_sprite.TXObj[txid].zt_tx_icon=="tx_qianghua"?"tx_hy":TX_sprite.TXObj[txid].zt_tx_icon);
			txSp.addChild(tsp);
			tsp.scaleX	/=	this.scaleX;
			tsp.scaleY	/=	this.scaleY;
			
			return tsp;
			
		}
		/**
		 *设置特效 
		 * @param txid	特效的id
		 * @param isTrue	是否清除
		 * @param delay		显示时间
		 * 
		 */
		public function setState(txid:uint, isTrue:Boolean, delay:uint=0, _rotation:Number=0):void
		{
			if(null==txStMap)return ;
			if(this.txSp==null){
				this.txSp	=	new Sprite;
				this.addChild(txSp);
			}
			
			if(!TX_sprite.TXObj.hasOwnProperty(txid))throw new Error("找不到的状态");
			if(isTrue){
				if(txStMap.hasOwnProperty(txid) && txStMap[txid]!=null)return;
				txStMap[txid]	=	addTX(txid,delay,_rotation);
			}else{
				if(!txStMap.hasOwnProperty(txid))return;
				
				if(null!=txStMap[txid] && this.txSp.contains(txStMap[txid]))this.txSp.removeChild(txStMap[txid]);
				txStMap[txid]		=	null;
			}
			
			if(delay!=0){
				var tm:Timer	=	new Timer(delay,1);
				tm.addEventListener(TimerEvent.TIMER, function():void{setState(txid,false);});
				tm.start();
			}
		}
		
		/**
		 *更新特效位置 
		 * @param sp
		 * @param txType	特效类型：1头顶2脚下12建筑14建筑
		 * 
		 */
		public function upDatePosition(sp:Sprite=null,txType:uint=2):void
		{
			if(sp==null)return;
			switch(txType){
				case 1:												//显示在头上，如箭头（不包括围墙）
					if(null!=this  && this is MN_Zombie && null!=(this as MN_Zombie).commonZombie){
						var head:DisplayObjectContainer		=	(this as MN_Zombie).getPart("part0");
						var foot:DisplayObjectContainer		=	(this as MN_Zombie).getPart("part12");
						
						sp.y	=	-(this as MN_Zombie).commonZombie.height+(head.y-head.height)*ViewCartoonTarget.scaleValue;
						sp.y   += (this as MN_Zombie).commonZombie.height-foot.y*ViewCartoonTarget.scaleValue;
//						sp.y = -(this as MN_Zombie).commonZombie.height + sp.height - 10;////*this.scaleY-sp.height;
					}
					else
						if(this is MT_HavenWall){
							sp.y-=sp.height;
						}
						else
							if(this is MT_Building_Sundry)
								sp.y = sp.y - sp.height;//	- sp.height;
					//else
					//sp.y = sp.y - this.height-sp.height;
					break;
				
				case 2:										//默认，僵尸包括（强光，强化，
					break;
				case 12:											//建筑升级12格
					sp.x	=	-20*12;//GV_Map.instance.gridSize*12;	
					sp.y   +=	20;
					break;
				case 14:											//建筑升级14格
					sp.x	=	-20*14;//GV_Map.instance.gridSize*12;
					sp.y   +=	20;
					break;
				break;
			}
		}
		
		/**
		 *设置多个特效 
		 * @param txids
		 * @param isTrue
		 * @param delay
		 * @param dir
		 * 
		 */
		public function setStates(txids:String, isTrue:Boolean, delay:uint=0, _rotation:Number=0):void
		{
			var txidsAry:Array		=	txids.split("^");
			for(var i:uint=0; i<txidsAry.length; i++)
			{
				this.setState(uint(txidsAry[i]), isTrue, delay, _rotation);
			}
		}
		
		public function releaseStates():void
		{
			if(txStMap==null)return;
			var ob:Sprite;
			for each(ob in txStMap)
			{
				if(ob!=null)
					this.txSp.removeChild(ob);
			}
			txStMap=null;
			if(this.txSp!=null)
				this.removeChild(this.txSp);
		}
		
	}
}